package org.liang.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import io.swagger.annotations.ApiOperation;
import org.liang.dto.TenantRegisterDto;
import org.liang.service.ITenantService;
import org.liang.domain.Tenant;
import org.liang.query.TenantQuery;
import org.liang.util.AjaxResult;
import org.liang.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;

    /**
     * 保存和修改公用的
     * @param tenant  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant){
        if( tenant.getId()!=null)
            tenantService.updateById(tenant);
        else
            tenantService.insert(tenant);
        return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        tenantService.deleteById(id);
        return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        Tenant tenant = tenantService.selectById(id);
        return AjaxResult.success(tenant);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){
        List< Tenant> list = tenantService.selectList(null);
        return AjaxResult.success(list);
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody TenantQuery query) {
        Page<Tenant> page = new Page<Tenant>(query.getPage(),query.getRows());
        page = tenantService.selectPage(page);
        PageList pageList = new PageList<Tenant>(page.getTotal(),page.getRecords());
        return AjaxResult.success(pageList);
    }

    /**
     * 租户入驻
     * @param dto
     * @return
     */
    @PostMapping("/tenantRegister")
    public AjaxResult tenantRegister(@RequestBody TenantRegisterDto dto) {
        tenantService.tenantRegister(dto);
        return AjaxResult.success();
    }

    //店铺入驻审核通过
    @ApiOperation(value = "更新当前店铺状态为通过")
    @PutMapping("/reviewAdopt")
    public AjaxResult reviewAdopt(@RequestBody Tenant tenant){
        tenantService.reviewAdopt(tenant);
        return AjaxResult.success(tenant);
    }

    @GetMapping("/activation/{emailName}")
    public AjaxResult Activation(@PathVariable String emailName){
        System.out.println(emailName);
        tenantService.activation(emailName);
        return AjaxResult.success();
    }

    //店铺入驻审核驳回
    @ApiOperation(value = "更新当前店铺状态为驳回")
    @PutMapping("/reviewReject")
    public AjaxResult reviewReject(@RequestBody Tenant tenant){
        tenantService.reviewReject(tenant);
        return AjaxResult.success();
    }
}
